package appx.craft.droid.uc;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import appx.craft.droid.R;
import appx.craft.droid.bean.FeedBean;

/**
 * 
 * This class is used to show pop up window in sliding screen bottom bar 
 *
 */
public class ActivityBottomBar implements OnClickListener {

	private final static String TAG = ActivityBottomBar.class
			.getCanonicalName();

	private Context mContext;
	
	private LayoutInflater mLayoutInflat = null;
	
	private PopupWindow mPopupWindow = null;
	
	public Button btn_Previous, btn_Next;

	public Button btn_save_local;
	
	private boolean mIsShowing = false;
	
	private ProgressBar toggleIndeterminate_top_bar;
	
	public boolean isShowing() { return mIsShowing; }
	
	private OnActivityMenuListener mCallBack;
	
	public ActivityBottomBar(Context context, LayoutInflater mLayoutInflater) {
		Log.d(TAG," [ ActivityBottomBar ]");
		mContext = (Context) context;
		mLayoutInflat = mLayoutInflater;
		mCallBack = (OnActivityMenuListener)context;
	}

	public synchronized void show(View v, int mPosition,
			ArrayList<FeedBean> mArrayList) {
		Log.d(TAG," [ show ]");
		mIsShowing = true;
		if (mPopupWindow != null)
			return;
		Display display = ((WindowManager) mContext
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		View mView = mLayoutInflat.inflate(R.layout.custom_bottom_menu, null);

		btn_Previous = (Button) mView.findViewById(R.id.btn_prv);
		btn_Next = (Button) mView.findViewById(R.id.btn_next);
		btn_Next.setOnClickListener(this);
		btn_Previous.setOnClickListener(this);
		
		btn_save_local = (Button)mView.findViewById(R.id.btn_save_local);
		btn_save_local.setOnClickListener(this);
		
		toggleIndeterminate_top_bar = (ProgressBar)mView.findViewById(R.id.toggleIndeterminate_top_bar);
		
		// Take decision for button visibility
		setNavigationControl(mPosition, mArrayList);

		mPopupWindow = new PopupWindow(mView,
				android.view.ViewGroup.LayoutParams.MATCH_PARENT, 65, false);
		mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		mPopupWindow.setWidth(display.getWidth());
		mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
	}

	public synchronized void hide() {
		
		mIsShowing = false;
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
			mPopupWindow = null;
		}
		return;
	}

	public void setNavigationControl(int mPosition,
			ArrayList<FeedBean> arrListImageBean) {
		
		if (isShowing()) {

			if (mPosition == 0) {
				btn_Previous.setVisibility(View.GONE);
				btn_Previous.setBackgroundResource(R.drawable.btn_prv_disabled);
				btn_Previous.setEnabled(true);
				btn_Next.setBackgroundResource(R.drawable.btn_next_selector);
				btn_Next.setVisibility(View.VISIBLE);

			} else if (mPosition > 0
					&& !(mPosition == arrListImageBean.size() - 1)) {
				btn_Next.setBackgroundResource(R.drawable.btn_next_selector);
				btn_Next.setVisibility(View.VISIBLE);
				btn_Previous.setBackgroundResource(R.drawable.btn_prv_selector);
				btn_Previous.setVisibility(View.VISIBLE);

			} else if (mPosition > 0
					&& mPosition == arrListImageBean.size() - 1) {
				btn_Next.setVisibility(View.GONE);
				btn_Next.setBackgroundResource(R.drawable.btn_next_disabled);
				btn_Next.setEnabled(true);
				btn_Previous.setBackgroundResource(R.drawable.btn_prv_selector);
				btn_Previous.setVisibility(View.VISIBLE);
			}
		}

	}

	@Override
	public void onClick(View v) {
		mCallBack.onButtonSelected(v.getId());
	}
	
	public void startSave(){
		
		if(toggleIndeterminate_top_bar != null && btn_save_local != null){
			btn_save_local.setVisibility(View.INVISIBLE);
			toggleIndeterminate_top_bar.setVisibility(View.VISIBLE);
		}
	}
	public void stopSave(){
		
		if(toggleIndeterminate_top_bar != null && btn_save_local != null){
			
			btn_save_local.setVisibility(View.VISIBLE);
			toggleIndeterminate_top_bar.setVisibility(View.INVISIBLE);
		}
		
	}
}
